package com.example.score.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.score.entity.Admin;
import com.example.score.entity.AdminRole;
import com.example.score.entity.Role;
import com.example.score.mapper.AdminMapper;
import com.example.score.service.AdminRoleService;
import com.example.score.service.AdminService;
import com.example.score.service.RoleService;
import com.example.score.util.ContextHolderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author shenrui
 * @date 2021/9/26
 * @description 实现AdminRoleService,继承ServiceImpl里面的增删改查方法，并继承UserDetailsService，
 * 设置spring security要用到loadUserByUsername
 */
@Service
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService, UserDetailsService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AdminRoleService adminRoleService;
    /**
     * @return UserDetails
     * @description 重写loadUserByUsername
     */
    @Override
    public UserDetails loadUserByUsername(String adminName) throws AuthenticationException {
        HttpServletRequest request = ContextHolderUtils.getRequest();
        String password = request.getParameter("adminPassword");
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        Map<String,Object> map = new HashMap<>();
        map.put("admin_name",adminName);
        List<Admin> admins = adminMapper.selectByMap(map);
        /**
         * 因为不想用security本身的msg消息，就提前做了一下想要的msg
         */
        if(adminName == null || password == null) {
            throw new BadCredentialsException("用户名或密码不能为空");
        }
        if(admins.size() == 0 || admins.get(0) == null) {
            throw new BadCredentialsException("帐号不存在，请重新输入");
        }
        if(!bcryptPasswordEncoder.matches(password, admins.get(0).getPassword())) {
            throw new BadCredentialsException("密码错误");
        }
        if(admins.get(0).getAdminStatus() == 0) {
            throw new BadCredentialsException("账号已被封禁");
        }
        List<AdminRole> adminRoles = adminRoleService.list(new LambdaQueryWrapper<AdminRole>().eq(AdminRole::getAdminId,admins.get(0).getId()));
        List<Long> roleIds = new ArrayList<>();
        for (AdminRole userRole : adminRoles) {
            roleIds.add(userRole.getRoleId());
        }
        List<Role> roles = roleService.list(new LambdaQueryWrapper<Role>().in(Role::getRoleId,roleIds));
        admins.get(0).setRoles(roles);
        return admins.get(0);
    }


}
